<template>
  <el-dialog title="分配角色" :visible="rolesDailog" @close="closeFn">
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in allRole" :key="item.id" :label="item.id">{{ item.title }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-row type="flex" justify="center">
        <el-button type="primary" size="small" @click="submit">确认</el-button>
        <el-button size="small" @click="closeFn">取消</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getRolesList, getEmployeesInfo, editEmployeesInfo } from '@/api/employees'
export default {
  props: {
    rolesDailog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      checkList: [],
      allRole: [],
      curEmpId: ''
    }
  },
  async created() {
    // 获取角色列表
    const res = await getRolesList()
    // console.log(res)
    this.allRole = res.data
  },
  methods: {
    // 分配角色
    async submit() {
      await editEmployeesInfo({
        id: this.curEmpId,
        roles: this.checkList
      })
      this.closeFn()
      this.$message({
        message: '分配成功',
        type: 'success'
      })
    },
    // 关闭弹窗
    closeFn() {
      this.$emit('update:rolesDailog', false)
      this.curEmpId = ''
      this.checkList = []
    },
    // 回显获取指定的角色id，
    async getRolesId(id) {
      this.curEmpId = id
      const res = await getEmployeesInfo(id)
      //   console.log(res)
      this.checkList = res.data.roles.map(item => item.id)
    }
  }

}
</script>

<style>

</style>
